Data Visualization Using Base Graphics

There's two primary apis for graphics in R. ggplot2 and base graphics (the built in plotting library)

This demo uses base because it's easier.


In [1]:
#Let's get some data
povertyDf = read.csv("./data/world_poverty_in_millions.csv")

In [2]:
#lets make the dates into years
getYear = function(x) { as.numeric(format(as.Date(x), '%Y')) }
povertyDf$Year = unlist(lapply(povertyDf$Date, getYear))

In [3]:
#order the data by date asc
povertyDf = povertyDf[order(povertyDf$Year, decreasing = FALSE),]

In [4]:
head(povertyDf, 5)


DateValueYear
141981-12-311903.70 1981
131984-12-311859.38 1984
121987-12-311746.55 1987
111990-12-311850.54 1990
101993-12-311855.44 1993

In [5]:
#resize the default plot size so we can actually read it on screen
options(repr.plot.width=8, repr.plot.height=4)

In [6]:
plot(povertyDf$Year, povertyDf$Value)



In [7]:
#A very basic barchart. Just the two data sets. 
barplot(povertyDf$Value, names.arg = povertyDf$Year)



In [8]:
# adding in some labels
barplot(povertyDf$Value, names.arg = povertyDf$Year, 
        main = "World Poverty in Millions", 
        xlab = "Year", ylab = "People in Poverty (Millions)")



In [9]:
#lets make things a little prettier using par()
par(bg = "#EDFBFF", las = 1, col.lab = "#262626", col.axis = "#262626",
    cex.axis = 0.9, cex.lab = 1.25, cex.main = 1.5)

In [10]:
#lets tweak the look a little bit more
barplot(povertyDf$Value, names.arg = povertyDf$Year,
    main = "World Poverty in Millions",
    xlab = "Year", ylab = "People in Poverty (Millions)",
    ylim = c(0, 2000), col = "#00B01A")



In [11]:
#let's try a line chart instead | type l = line chart
plot(povertyDf$Year, povertyDf$Value, type = "l",
    main = "World Poverty in Millions",
    xlab = "Year", ylab = "People in Poverty (Millions)",
    ylim = c(0, 2000), xlim = c(1980, 2013), col = "#00475E", lwd = 3)



In [12]:
# we can save plots too | Supported formats include: svg(), pdf(), jpeg(), png()...
png("data/Poverty_In_Millions.png", width = 800, height = 1000)
barplot(povertyDf$Value, names.arg = povertyDf$Year,
    main = "World Poverty in Millions",
    xlab = "Year", ylab = "People in Poverty (Millions)",
    ylim = c(0, 2000), col = "#00B01A")
dev.off() #turns off the plot saving


png: 2